package cn.tedu.springdemo.day02;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 针对day包下bean进行测试
 * 重载方法一：getbean通过id获取
 * 找到唯一的---->返回bean （正确）
 * 找不到------->NoSuchBeanDefinitionException 异常 没有对应的Bean
 * 找到多个----->不可以找到多个因为id不可能配置多个相同
 */
public class Test01 {
    @Test
    public void test01(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext4.xml");
        Person p=(Person)ac.getBean("person");
        System.out.println(p);
        ((ClassPathXmlApplicationContext)ac).close();
    }
    /**
     * 针对day包下bean进行测试
     * 重载方法二：getbean通过“class”类获取
     * 找到唯一的---->返回bean （正确）
     * 找不到------->NoSuchBeanDefinitionException 异常 没有对应的Bean定义：class
     * 找到多个----->NoUniqueBeanDefinitionException 异常 没有唯一对应的bean
     */
    @Test
    public void test02(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext4.xml");
       Car c= ac.getBean(Car.class);
        System.out.println(c);
        ((ClassPathXmlApplicationContext)ac).close();
    }
    /**SpringIOC控制反转
     * 别名标签
     */
    @Test
    public void test03(){
      ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext5.xml");
      //通过id
      Pig p=(Pig)ac.getBean("pig");
      System.out.println(p);
      //通过标签
        Pig p1=(Pig)ac.getBean("peiqi");
        System.out.println(p1);
      ((ClassPathXmlApplicationContext)ac).close();
    }
    //SpringIOC控制反转：创建对象时候---构造器
    //1.SpringIOC创建对象：默认调用无参数构造器
    @Test
    public void test04() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext5.xml");
        Tom tom=(Tom)ac.getBean("tom");
        System.out.println("tom");
        ((ClassPathXmlApplicationContext)ac).close();
    }
    @Test
    public void test05(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext6.xml");
        Demo demo=(Demo)ac.getBean("demo");
        System.out.println(demo);
        ((ClassPathXmlApplicationContext)ac).close();
    }
}
